<?php
require_once("../web_config.php");
require_once("XmlBlocks.php");
require_once("XmlSend.php");
//require_once("XmlUtils.php");
require_once(APPLICATION_ROOT."/oracleaccess/classes/DbConnect.php");
require_once(APPLICATION_ROOT."/scadenzario/aggiorna/utility.class");
require_once(APPLICATION_ROOT."/logs/LogApp.class" );

class XmlDeleteApp extends XmlBlocks {
	
	static $instance;
	private $conn;	
	private $xml;
	private $file;
	private $file_destination;
	
	private function __construct() {
	
		$this->conn = new DbConnect();

	}

	static function getInstance()
	{
		
		if(self::$instance == NULL){
			
			self::$instance = new XmlDeleteApp();
		}
		return self::$instance;
	}

	final public function Generate($array_schede) 
	{

		foreach($array_schede as $scheda)
		{

  	$this->cig=$scheda["CIG"]; 
  	$this->xml=$this->__Intestazione();				
			
   //$cigtest=$scheda["CIG"];
			$CIG=$this->WorkCig("CIG",$scheda["CIG"],"Y");
			$CUI=$scheda["CUI"];
			$NOME_SCHEDA=strtoupper($scheda["SCHEDA"]);
			$NUM_PROGR=$scheda["NUM_PROGR"];
			$NUM_PROGR_SCHEDA=$scheda["NUM_PROGR_SCHEDA"];
			//$ID_SCHEDA_SIMOG=$scheda["ID_SCHEDA_SIMOG"];
			$ID_SCHEDA_LOCALE=$scheda["ID_SCHEDA_LOCALE"];
			
			//SCRIVERE LOG
			$message["SCHEDA"]=$NOME_SCHEDA;
			$message["CIG"]=$CIG;
			$message["CUI"]=$CUI;
			$message["NUM_PROGR"]=$NUM_PROGR;
			$message["NUM_PROGR_SCHEDA"]=$NUM_PROGR_SCHEDA;
			$message["ID_SCHEDA_LOCALE"]=$ID_SCHEDA_LOCALE;
			//$message["ID_SCHEDA_SIMOG"]=$ID_SCHEDA_SIMOG;
			$message["OPERAZIONE"]="XmlDeleteApp->Generate";
			$message["MSG"]="ELIMINAZIONE SCHEDA AVCPOSIT XML";
			LogApp::getInstance()->LogWrite($message);

			$this->CreateXmlScheda($scheda);
		 
   $this->xml.=$this->__closeXml();
		
		 $this->WriteXml();

		 //la variabile check indica se il file xml ha passato la validazione oppure no
		 $this->VerifyXml();		
   		
		}
		
		return $this->check;

	}

	final public function WriteXml() {
		
		
		$microtime = microtime();
		$today = date("YmdHis").$microtime[2].$microtime[3];                           
    
    //Il nome del file ha la seguente struttura: CIG _NP+NUM PROG_NPS+NUM PROG SCHEDA__SIGLE SCHEDE INVIATE + DATETIME.XML
		$this->file=APPLICATION_ROOT.'/authority/tmp/ELIMINAZIONE_'.$this->cig."_".$today.'.xml';
		
		$out = fopen($this->file, "w");
		fwrite($out,$this->xml);
		fclose($out);
		
	}
	
	final public function MoveXml() {
		
		$microtime = microtime();
		$today = date("YmdHis").$microtime[2].$microtime[3];                           

		$this->file_destination=APPLICATION_ROOT.'/authority/xml/ELIMINAZIONE_'.$this->cig."_".$today.'.xml';
		
		rename($this->file,$this->file_destination);
		
	}

	final public function DelXml() {

		unlink($this->file_destination);
		
	}
  	
	final public function CreateXmlScheda($scheda)
	{
		
		//Inseriamo i valori in sessione prima che avvengano i controlli sui campi
		$_SESSION['XML__SCHEDA']=$scheda["SCHEDA"];
		$_SESSION['XML__CIG']=$scheda["CIG"];
		
		$CIG=$this->WorkCig("CIG",$scheda["CIG"],"Y");
		
		
		if(!empty($scheda["CUI"]))
		{
		  $CUI=$this->WorkCUI($scheda["CUI"]);
	    }
	    
	    if(!empty($scheda["SCHEDA"]) && $scheda["SCHEDA"]!='GARE' && $scheda["SCHEDA"]!='GARE_SS')
		{
	      //ELIMINIAMO DA REPORT_AVCPOSIT
	      $del="DELETE FROM REPORT_AVCPOSIT WHERE CIG='".$scheda["CIG"]."' AND NUM_PROGR='".$scheda["NUM_PROGR"]."' AND NOME_SCHEDA='".$scheda["SCHEDA"]."'";	
          $this->conn-> doquery_oracle($del,'delete');		
          	
		  $NOME_SCHEDA=$this->WorkTipiSchedeType("SCHEDA",strtoupper(trim($scheda["SCHEDA"])));
	    }
	    else if($scheda["SCHEDA"]=='GARE' or $scheda["SCHEDA"]=='GARE_SS')
	    {
	     //ELIMINIAMO DA REPORT_AVCPOSIT
	     $del="DELETE FROM REPORT_AVCPOSIT WHERE CIG='".$scheda["CIG"]."'";	
	     $this->conn-> doquery_oracle($del,'delete');		    
		  $NOME_SCHEDA='GARE';
	    }
	   
	    
	    if(!empty($scheda["ID_SCHEDA_LOCALE"])){
		    $ID_SCHEDA_LOCALE=$this->WorkIdSchedaType("ID_SCHEDA_LOCALE",$scheda["ID_SCHEDA_LOCALE"]);
	    }

     $this->xml.='<SchedeEliminate tns:CIG="'.$CIG.'" tns:CUI="'.$CUI.'"';
        
        
		 
     if($NOME_SCHEDA!="" && $NOME_SCHEDA!="GARE" && $NOME_SCHEDA!="AGGIUDICAZIONE" && $NOME_SCHEDA!="AGGIUDICAZIONE_SS"){
		    $this->xml.=' tns:SCHEDA="'.$NOME_SCHEDA.'"';
		   }
		 
		if($ID_SCHEDA_SIMOG!="")
		{
		 // $this->xml.=' tns:ID_SCHEDA_SIMOG="'.$ID_SCHEDA_SIMOG.'"';
		}

		if($ID_SCHEDA_LOCALE!="" && $NOME_SCHEDA!="AGGIUDICAZIONE" && $NOME_SCHEDA!="GARE" && $NOME_SCHEDA!="AGGIUDICAZIONE_SS" && $NOME_SCHEDA!="GARE_SS")
		{
		  $this->xml.=' tns:ID_SCHEDA_LOCALE="'.$ID_SCHEDA_LOCALE.'"';
		}
		 		 
		$this->xml.=' />'; 
		
		
	}


	final public function VerifyXml() {
		
		$xml = new DOMDocument();
		$xml->load($this->file);
		
		$tempDom = new DOMDocument();
		
		//OMETTIAMO LA VISUALIZZAZIONE DEGLI ERRORI DI PARSING A VIDEO
		ini_set('display_errors', 0);		
		
		$tempDom->loadXML(utf8_encode($xml->saveXML()));

		if ($tempDom->schemaValidate(APPLICATION_ROOT.'/authority/xsd/TrasferimentoScheda_A.xsd'))
		{
	    //echo "XML OK";
	    
	    $this->MoveXml();
	    
			//INVIO XML (SFTP AVCPOSIT) - Dopo la funzione "MoveXml"
			////////////////////			
			$check=XmlSend::getInstance()->XmlSendToAVCPOSIT();	
			//SE NON E' POSSIBILE COLLEGARSI AL SERVER SFTP AVCPOSIT ELIMINIAMO IL FILE XML
      if($check=="N"){
        $this->DelXml();  
        die('Impossibile collegarsi al server SFTP AVCPOSIT');         
      }else{
  			//SCRIVERE LOG
  			$message["SCHEDA"]=$this->scheda;
  			$message["CIG"]=$this->cig;
  			$message["NUM_PROGR"]=$this->num_progr;
  			$message["NUM_PROGR_SCHEDA"]=$this->num_progr_scheda;
  			$message["OPERAZIONE"]="XmlDeleteApp->Generate";
  			$message["MSG"]="FINE GENERAZIONE XML";
  			LogApp::getInstance()->LogWrite($message);
  			$this->check="Y";      
      } 
    
		} else {

	    //echo "XML NON VALIDO";
			//SCRIVERE LOG
			$message["SCHEDA"]=$this->scheda;
			$message["CIG"]=$this->cig;
			$message["NUM_PROGR"]=$this->num_progr;
			$message["NUM_PROGR_SCHEDA"]=$this->num_progr_scheda;
			$message["OPERAZIONE"]="XmlDeleteApp->Generate";
			$message["MSG"]="ERRORE GENERAZIONE XML";
			LogApp::getInstance()->LogWrite($message);
			$this->check="N"; 		    
		}
		
	}

}
?>